package com.zjx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zjx.reggie.common.R;
import com.zjx.reggie.entity.AddressBook;
import com.zjx.reggie.entity.ShoppingCart;
import com.zjx.reggie.entity.User;
import com.zjx.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;
    
    @GetMapping("/default")
    public R shoppingCart(HttpServletRequest request){
        String userId =(String) request.getSession().getAttribute("userId");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    @PostMapping()
    public R addressAdd(@RequestBody AddressBook addressBook, HttpServletRequest request){
        String userId =(String) request.getSession().getAttribute("userId");
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        addressBook.setIsDeleted(0);

        addressBookService.save(addressBook);
        return R.success("收货地址添加成功！");
    }

    @GetMapping("/list")
    public R list(HttpServletRequest request){
        String userId =(String) request.getSession().getAttribute("userId");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    @PutMapping("/default")
    public R shoppingCart(@RequestBody AddressBook addressBook){
        log.info(addressBook.toString());
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault,0);
        addressBookService.update(qw);

        qw.eq(AddressBook::getId,addressBook.getId());
        qw.set(AddressBook::getIsDefault,1);
        addressBookService.update(qw);
        return R.success("默认地址设置成功！");
    }

    @GetMapping("/{id}")
    public R updateAddress(@PathVariable String id){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getId,id);
        AddressBook one = addressBookService.getOne(qw);
        return R.success(one);
    }

    @PutMapping()
    public R update(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }

    @DeleteMapping("")
    public R delete(HttpServletRequest request,String ids){
        addressBookService.removeById(ids);
        String userId = (String) request.getSession().getAttribute("userId");


        LambdaQueryWrapper<AddressBook> addressQw = new LambdaQueryWrapper<>();
        addressQw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(addressQw);
        list.get(0).setIsDeleted(1);

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getId,list.get(0).getId());
        qw.set(AddressBook::getIsDefault,1);

        addressBookService.update(qw);

        return R.success("删除成功！");
    }
}
